package com.bytz.modules.cms.user.controller;

import com.boya.cup.flowable.service.model.enumeration.AcceptAction;
import com.boya.cup.flowable.service.model.response.auditpage.AuditPageModel;
import com.bytz.common.api.vo.Result;
import com.bytz.common.aspect.annotation.AutoLog;
import com.bytz.modules.bpm.client.controller.BizFlowControllerBase;
import com.bytz.modules.bpm.client.model.response.StartFlowResponse;
import com.bytz.modules.cms.user.entity.EcAudit;
import com.bytz.modules.cms.user.model.EcAuditEntity;
import com.bytz.modules.cms.user.model.EcAuditSaveRo;
import com.bytz.modules.cms.user.service.impl.EcAuditFlowServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashSet;
import java.util.List;

/**
 * @program: cms-backend
 * @description:
 * @author: KyleWang
 * @create: 2022-10-31 14:11
 **/
@RestController
@Api(tags = "ec订单审批流程")
@RequestMapping("/bizflow/work_flow/cms_order_ec")
public class EcAuditFlowController extends BizFlowControllerBase<EcAudit, EcAuditSaveRo, EcAuditEntity, EcAuditFlowServiceImpl> {

    @Override
    @AutoLog("提交流程")
    @ApiOperation(
            value = "提交流程",
            notes = "提交流程"
    )
    @PostMapping({"/submitProcess" })
    public Result<StartFlowResponse> submitProcess(EcAuditSaveRo entity) throws Exception {
        return Result.error("不支持该方法");
    }

    @AutoLog("审批通过")
    @ApiOperation(
            value = "审批通过",
            notes = "审批通过"
    )
    @PostMapping({"/completeTask" })
    @Override
    public Result<Boolean> completeTask(EcAuditEntity auditEntity) throws Exception {
        Result<AuditPageModel> taskAuditPageInfoModel = getTaskAuditPageInfoModel(auditEntity);
        if (taskAuditPageInfoModel != null
                && taskAuditPageInfoModel.isSuccess()) {
            AuditPageModel auditPageModel = taskAuditPageInfoModel.getResult();
            List<AcceptAction> acceptActions = auditPageModel.getAcceptActions();
            if (CollectionUtils.isNotEmpty(acceptActions
            )) {
                HashSet<AcceptAction> acceptActionsSet = new HashSet<>(acceptActions);
                if (acceptActionsSet.contains(AcceptAction.CLAIM)) {
                    claimTask(auditEntity);
                }
            }
        }
        return super.completeTask(auditEntity);
    }
}